<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryParams"
        size="small"
        label-width="88px"
        class="formBox"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="充值单号" prop="rechargeOrderNo">
              <el-input
                v-model="queryParams.rechargeOrderNo"
                placeholder="请输入充值单号"
                clearable
                 maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入姓名"
                clearable
                maxlength="50"
                show-word-limit  
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="身份证号" prop="idCard">
              <el-input
                v-model="queryParams.idCard"
                placeholder="请输入身份证号"
                clearable
                maxlength="18"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select
                v-model="queryParams.paymentMethod"
                placeholder="请选择支付方式"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.payment_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="请选择状态"
                style="width: 100%"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="支付时间" prop="time">
              <el-date-picker
                v-model="queryParams.time"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 按钮 -->
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['order:records:export']"
            >导出</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="recordsQueryFun"
            v-hasPermi="['order:records:refund']"
            >同步支付结果
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <!-- 列表 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="recordsList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="充值单号"
          align="center"
          prop="rechargeOrderNo"
          width="180"
        />
        <el-table-column label="姓名" align="center" prop="name" width="140" />
        <el-table-column label="身份证号" align="center" prop="idCard" width="180" />
        <el-table-column label="学校" align="center" prop="school" />
        <el-table-column label="年级" align="center" prop="grade" />
        <el-table-column label="班级" align="center" prop="className" />
        <el-table-column label="身份" align="center" prop="identity">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.personnel_identity"
              :value="scope.row.identity"
            />
          </template>
        </el-table-column>

        <el-table-column label="充值金额" align="center" prop="rechargeAmount">
          <template slot-scope="scope">
            {{ scope.row.rechargeAmount ? scope.row.rechargeAmount : "0" }}元
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="支付方式" align="center" prop="paymentMethod">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.payment_method"
              :value="scope.row.paymentMethod"
            />
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdAt, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="失败原因" align="center" prop="message" />
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
          v-if="userId != '109' && userId != '108'"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="padding_4_10"
              @click="refundStatusFun(scope.row)"
              v-if="scope.row.status == 4 && scope.row.finish != 1"
              v-hasPermi="['order:records:refund']"
              >申请退款
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog title="申请退款" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="充值订单号" prop="rechargeOrderNo">
          <el-input
            v-model="form.rechargeOrderNo"
            placeholder="请输入充值订单号"
            readonly
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" readonly />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" readonly />
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校" readonly />
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" readonly />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount" class="is-required">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod" class="is-required">
          <el-select v-model="form.paymentMethod" placeholder="请选择支付方式" clearable>
            <el-option
              v-for="dict in dict.type.payment_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="退款理由" prop="refundReason" class="is-required">
          <el-input v-model="form.refundReason" placeholder="请输入退款理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecords,
  getRecords,
  delRecords,
  addRecords,
  updateRecords,
  recordsRefund,
  queryByOrderIds,
} from "@/api/order/records";

export default {
  name: "Records",
  dicts: ["personnel_identity", "order_status", "payment_method"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idCard: null,
        school: null,
        grade: null,
        class: null,
        identity: null,
        rechargeOrderNo: null,
        platformOrderNo: null,
        channelOrderNo: null,
        rechargeAmount: null,
        status: null,
        paymentMethod: null,
        paymentTime: null,
        createdAt: null,
        time: [],
      },
      form: {},

      userId: "",
    };
  },
  watch: {
    "queryParams.time": function (newVal) {
      console.log(newVal);
      if (!newVal) {
        this.queryParams.time=[];
        this.queryParams.paymentTimeStart = null;
        this.queryParams.paymentTimeEnd = null;
      }
    },
  },
  created() {
    this.userId = window.sessionStorage.getItem("userId");
    this.getList();
  },
  
  activated() {
    this.$forceUpdate();
  },
  methods: {
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.time.length == 2) {
        this.queryParams.paymentTimeStart = this.queryParams.time[0];
        this.queryParams.paymentTimeEnd = this.queryParams.time[1];
      }
      listRecords(this.queryParams).then((response) => {
        this.recordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    recordsQueryFun() {
      var that = this;
      if (this.ids.length == 0) {
        return this.$modal.msgError("请选择订单");
      }
      var data = {
        ids: this.ids,
      };
      queryByOrderIds(data).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess(response.msg);
          that.getList();
        } else {
          this.$modal.msgError(response.msg);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        idCard: null,
        personId: null,
        school: null,
        grade: null,
        class: null,
        identity: null,
        rechargeOrderNo: null,
        platformOrderNo: null,
        channelOrderNo: null,
        rechargeAmount: null,
        status: null,
        paymentMethod: null,
        paymentTime: null,
        createdAt: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.queryParams.paymentTimeStart = null;
      this.queryParams.paymentTimeEnd = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.form.refundAmount) {
        return this.$modal.msgError("请输入正确的退款金额");
      }
      if (!this.form.paymentMethod) {
        return this.$modal.msgError("请选择支付方式");
      }
      if (!this.form.refundReason) {
        return this.$modal.msgError("请输入退款理由");
      }
      this.$refs["form"].validate((valid) => {
        if (this.form.id != null) {
          recordsRefund(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          recordsRefund(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除充值记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRecords(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "order/records/export",
        {
          ...this.queryParams,
        },
        "充值记录" + `_${new Date().getTime()}.xlsx`
      );
    },
    refundStatusFun(options) {
      this.form = options;
      this.open = true;
    },
  },
};
</script>
<style>
.padding_4_10 {
  padding: 4px 10px !important;
}
</style>
